/*!--------------------------------*\
   3-Jekyll Theme
   @author Peiwen Lu (P233)
   https://github.com/P233/3-Jekyll
\*---------------------------------*/


// Detect window size, if less than 1280px add class 'mobile' to sidebar therefore it will be auto hide when trigger the pjax request in small screen devices.
if (isMobile && window.location.pathname != "/") {
    $('#sidebar').addClass('mobile fullscreen')
    $('#icon-arrow').addClass('fullscreen')
}
if (window.location.pathname == "/") {
    $("time.post-meta-date").hide();
}


// Search
$('#search-input').on('input', function (e) {
    var blogs = $(".pl__all").filter(function () {
        var reg = new RegExp(e.target.value, "i");
        return reg.test($(this).text());
    });
    $(".pl__all").hide();
    blogs.fadeIn(350);
});
// Variables
var sidebar = $('#sidebar'),
    container = $('#post'),
    content = $('#pjax'),
    button = $('#icon-arrow');

// Tags switcher
var clickHandler = function (id) {
    return function () {
        $(this).addClass('active').siblings().removeClass('active');
        $('.pl__all').hide();
        $('.' + id).delay(50).fadeIn(350);
    }
};

$('#tags__ul li').each(function (index) {
    $('#' + $(this).attr('id')).on('click', clickHandler($(this).attr('id')));
});

// If sidebar has class 'mobile', hide it after clicking.
$('.pl__all').on('click', function () {
    $(this).addClass('active').siblings().removeClass('active');
    if (sidebar.hasClass('mobile')) {
        $('#sidebar, #pjax, #icon-arrow').addClass('fullscreen');
    }
});

// Enable fullscreen.
$('#js-fullscreen').on('click', function () {
    if (button.hasClass('fullscreen')) {
        sidebar.removeClass('fullscreen');
        button.removeClass('fullscreen');
        content.delay(300).queue(function () {
            $(this).removeClass('fullscreen').dequeue();
        });
    } else {
        sidebar.addClass('fullscreen');
        button.addClass('fullscreen');
        content.delay(200).queue(function () {
            $(this).addClass('fullscreen').dequeue();
        });
    }
});

$('#mobile-avatar').on('click', function () {
    $('#sidebar, #pjax, #icon-arrow').addClass('fullscreen');
});

// Pjax
$(document).pjax('#avatar, #mobile-avatar, .pl__all', '#pjax', {fragment: '#pjax', timeout: 10000});
$(document).on({
    'pjax:click': function () {
        content.removeClass('fadeIn').addClass('fadeOut');
        NProgress.start();
    },
    'pjax:start': function () {
        content.css({'opacity': 0});
    },
    'pjax:end': function () {
        NProgress.done();
        container.scrollTop(0);
        content.css({'opacity': 1}).removeClass('fadeOut').addClass('fadeIn');
        afterPjax();
    }
});

// Re-run scripts for post content after pjax
function afterPjax() {
    // Open links in new tab
    $('#post__content a').attr('target', '_blank');

    // Generate post TOC for h1 h2 and h3
    var toc = $('#post__toc-ul');
    // Empty TOC and generate an entry for h1
    toc.empty().append('<li class="post__toc-li post__toc-h1"><a href="#post__title" class="js-anchor-link">' + $('#post__title').text() + '</a></li>');

    // Generate entries for h2 and h3
    $('#post__content').children('h2,h3').each(function () {
        // Generate random ID for each heading
        $(this).attr('id', function () {
            var ID = "",
                alphabet = "abcdefghijklmnopqrstuvwxyz";

            for (var i = 0; i < 5; i++) {
                ID += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
            }
            return ID;
        });

        if ($(this).prop("tagName") == 'H2') {
            toc.append('<li class="post__toc-li post__toc-h2"><a href="#' + $(this).attr('id') + '" class="js-anchor-link">' + $(this).text() + '</a></li>');
        } else {
            toc.append('<li class="post__toc-li post__toc-h3"><a href="#' + $(this).attr('id') + '" class="js-anchor-link">' + $(this).text() + '</a></li>');
        }
    });
    toc.append('<li class="post__toc-li post__toc-h2"><a href="#tag_h2" class="js-anchor-link">分类标签</a></li>');
    toc.append('<li class="post__toc-li post__toc-h2"><a href="#post__share" class="js-anchor-link">分享按钮</a></li>');
    toc.append('<li class="post__toc-li post__toc-h2"><a href="#idc-container-parent" class="js-anchor-link">精彩评论</a></li>');
    // Smooth scrolling
    $('.js-anchor-link').on('click', function () {
        var target = $(this.hash);
        container.animate({scrollTop: target.offset().top + container.scrollTop() - 70}, 500, function () {
            target.addClass('flash').delay(700).queue(function () {
                $(this).removeClass('flash').dequeue();
            });
        });
    });

    // Lazy Loading Disqus
    // https://jsfiddle.net/dragoncrew/SHGwe/1/
    var ds_loaded = false;
    //top = $('#disqus_thread').offset().top;
    //window.disqus_shortname = $('#disqus_thread').attr('name');

    function check() {
        if (!ds_loaded && container.scrollTop() + container.height() > top) {
            $.ajax({
                type: 'GET',
                url: 'https://' + disqus_shortname + '.disqus.com/embed.js',
                dataType: 'script',
                cache: true
            });
            ds_loaded = true;
        }
    }

    check();
    container.scroll(check);
}

//afterPjax();

